/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean.session;

import entity.DataObjectEntity;
import entity.LocationQuantityEntity;
import entity.RawMaterialEntity;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

/**
 *
 * @author wei
 */
@Stateless
public class RawMaterialEntityFacade extends AbstractFacade<RawMaterialEntity> {

    @PersistenceContext(unitName = "MacTeam-warPU")
    private EntityManager em;

    protected EntityManager getEntityManager() {
        return em;
    }

    public RawMaterialEntityFacade() {
        super(RawMaterialEntity.class);
    }

    public List findAllRM() {
        CriteriaBuilder builder = getEntityManager().getCriteriaBuilder();
        javax.persistence.criteria.CriteriaQuery cq = builder.createQuery();
        Root<DataObjectEntity> locationQuantityRoot = cq.from(LocationQuantityEntity.class);
        cq.select(locationQuantityRoot);
        cq.where(builder.equal(locationQuantityRoot.get("archived"), false));
        return new ArrayList(getEntityManager().createQuery(cq).getResultList());
    }

//    public List findAllRMInLocation(Long rmLocationID) {
//        CriteriaBuilder builder = getEntityManager().getCriteriaBuilder();
//        javax.persistence.criteria.CriteriaQuery cq = builder.createQuery();
//        Root<DataObjectEntity> locationQuantityRoot = cq.from(LocationQuantityEntity.class);
//        cq.select(locationQuantityRoot);
//        cq.where(builder.equal(locationQuantityRoot.get("location").get("id"), rmLocationID));
//        return new ArrayList(getEntityManager().createQuery(cq).getResultList());
//    }
    
          public List findAllRMInLocation(Long rmLocationID) {
      CriteriaBuilder builder = getEntityManager().getCriteriaBuilder();
            javax.persistence.criteria.CriteriaQuery cq = builder.createQuery();
            Root<DataObjectEntity> locationQuantityRoot = cq.from(LocationQuantityEntity.class);
        
            Predicate predicate1 = builder.equal(locationQuantityRoot.get("rawMaterialsLocation").get("id"), rmLocationID);
            Predicate predicate2 = builder.equal(locationQuantityRoot.get("archived"), false);
            
               cq.select(locationQuantityRoot);
               cq.where(builder.and(predicate1, predicate2));
            return new ArrayList(getEntityManager().createQuery(cq).getResultList());
    }

    public List< RawMaterialEntity> findAllRMs() {
        try {
            Query query = (Query) em.createQuery(
                    "SELECT r FROM RawMaterial r");
            List< RawMaterialEntity> result = (List< RawMaterialEntity>) query.getResultList();

            return result;
        } catch (NoResultException e) {
            return null;
        }
    }
        public RawMaterialEntity findRawMaterial(String rightName) {
        try {
            Query query = (Query) em.createQuery(
                    "SELECT r FROM RawMaterial r WHERE r.productName = '" + rightName + "'");
            RawMaterialEntity result = (RawMaterialEntity) query.getSingleResult();

            return result;
        } catch (NoResultException e) {
            return null;
        }
    }

    

    public boolean checkUserNameExist(String materialName, List<RawMaterialEntity> materials) {
        for(RawMaterialEntity r:materials){
            if(r.getProductName().equals(materialName)){
                return true;
            }
        }
        return false;
    }
}
